<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <style>
        * {
            list-style: none;
            margin: 0;
            padding: 0;
        }

        #shop-car li {
            border: 1px dotted red;
            margin: 20px 0;
            padding: 20px;
        }

        .container {
            width: 400px;
            padding: 20px;
        }
    </style>

    <div class="container">
        <ul id="shop-car">
           
        </ul>
        <p id="totalnum">总数量：</p>
        <p id="totalprice">总价格：</p>
    </div>

    <script>
let products = [
        {
            goods_name: '小米10-1',
            goods_img: 'https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg',
            goods_price: '500',
            goods_attr: '砂石黑4GB+64GB',
            goods_num: 10,
            goods_is_checked: true
        },
        {
            goods_name: '小米10-2',
            goods_img: 'https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg',
            goods_price: '510',
            goods_attr: '砂石黑4GB+64GB',
            goods_num: 50,
            goods_is_checked: false
        }
]


        // 渲染数据
        var shopCar =document.querySelector('#shop-car')
        products.forEach(item=>{
            shopCar.innerHTML+=`
            <li>
                <input type="checkbox" ${item.goods_is_checked?"checked":""} />
                <div>商品名称:${item.goods_name}</div>
                <img src="${item.goods_img}" alt="">
                <div >商品价格:<span class='price'>${item.goods_price}</sapn></div>
                <div >购买数量:<span class="num">${item.goods_num}</span></div>
                <div>商品规格: ${item.goods_attr}</div>
                <div class="check">是否选中:${item.goods_is_checked?'选中':'未选中'}</div>
            </li>
            `
        })

        // 求总数量
        var ipt=document.querySelectorAll('input');
        var num=document.querySelectorAll('.num')
        var price=document.querySelectorAll('.price')
        var totalnum=document.querySelector('#totalnum')
        var totalprice=document.querySelector('#totalprice')
        var check=document.querySelectorAll('.check')
        var allNum=0;
        var allPrice=0;
        // 初始数量和价格函数
        function data(i){
            allNum+=parseInt(num[i].innerHTML);
            allPrice+=parseInt(price[i].innerHTML);
            totalnum.innerHTML=`总数量:${allNum}`;
            totalprice.innerHTML=`总价格:${allPrice}`;
        }

        // 初始渲染
            for (let i = 0; i < ipt.length; i++) {
            if(ipt[i].checked==true){
               data(i)
            }
        }

        // 添加点击事件
        for (let i = 0; i < ipt.length; i++) {
            ipt[i].onclick=function(){
                if(ipt[i].checked==true){
                    data(i)
                // 选中状态
                check[i].innerHTML='是否选中：选中'
                }else{
                allNum-=parseInt(num[i].innerHTML);
                allPrice-=parseInt(price[i].innerHTML);
                totalnum.innerHTML=`总数量:${allNum}`;
                totalprice.innerHTML=`总价格:${allPrice}`;
                // 选中状态
                check[i].innerHTML='是否选中：未选中'
                }
            }
        }
    </script>
</body>
</html>